package com.example.tuter.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.tuter.R;
import com.example.tuter.entity.Post;
import com.example.tuter.util.TimeUnitUtil;

import java.util.ArrayList;

//我的收藏
public class MyCollectAdapter extends RecyclerView.Adapter<MyCollectAdapter.MyCollectViewHolder> {
    private ArrayList<Post> data;
    private Context context;
    private final int TYPE_ITEM = 1;
    private final int TYPE_FOOTER = 2;

    public MyCollectAdapter(ArrayList<Post> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public MyCollectViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_floor_info, parent, false);
            return new MyCollectViewHolder(view, viewType);

        } else if (viewType == TYPE_FOOTER) {
            View footview = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.no_content_more, parent, false);
            return new MyCollectViewHolder(footview, viewType);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull MyCollectViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_ITEM) {
            Post post = data.get(position);
            holder.username.setText(post.getUsername());
            holder.content.setText(post.getDescription());
            holder.floors.setText("");
            String time = "丢了";
            try {
                time = TimeUnitUtil.getTimeByTimestampString(Long.valueOf(post.getCreateTime()));
            }catch (Exception ignored){

            }
            holder.time.setText(time);
            holder.comment.setText(post.getFloors());
            Glide.with(context).load(post.getPic1()).crossFade().diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.pic1);
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为FooterView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public class MyCollectViewHolder extends RecyclerView.ViewHolder {
        private TextView username;
        private TextView comment;
        private TextView content;
        private TextView floors;
        private TextView time;
        private ImageView pic1;
        LinearLayout footView;

        public MyCollectViewHolder(@NonNull View itemView, int viewType) {
            super(itemView);
            if (viewType == TYPE_ITEM) {
                username = itemView.findViewById(R.id.tie_info_username);
                content = itemView.findViewById(R.id.tie_info_content);
                floors = itemView.findViewById(R.id.tie_info_floors);
                time = itemView.findViewById(R.id.tie_info_convert_time);
                pic1 = itemView.findViewById(R.id.tie_info_imageView);
                comment = itemView.findViewById(R.id.post_info_comment_num);
                itemView.setOnClickListener(view -> {
                    if (monItemClickListener != null) {
                        monItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                });
            } else if (viewType == TYPE_FOOTER) {
                footView = (LinearLayout) itemView;
            }

        }
    }

    private OnRecyclerItemClickListener monItemClickListener;

    public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
        monItemClickListener = listener;
    }

    public interface OnRecyclerItemClickListener {
        void onRecyclerItemClick(int position);
    }
}
